<template>
    <div class="main">
      <div class="table">
          <div class="chhead">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/managemain' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>机房设置</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="input">
                <el-input
                v-model="search"  @click="handleEdit(scope.$index, scope.row)"
                size="mini"
                prefix-icon="el-icon-search"
                placeholder="输入关键字搜索" class="input" />
            </div>
          </div>
          <el-table
          :data="tableData.filter(data => !search || data.location.toLowerCase().includes(search.toLowerCase()))"
          style="width: 100%">
          <el-table-column
                label="机房类型"
                prop="type">
          </el-table-column>
          <el-table-column
                label="机房位置"
                prop="location">
          </el-table-column>
          <el-table-column
                label="机房名称"
                prop="name">
          </el-table-column>
          <el-table-column
              label="机房大小"
              prop="size">
          </el-table-column>
          <el-table-column
              label="机房状态">
              <template slot-scope="scope">
                  <span v-if="scope.row.status=='1'">{{"使用"}}</span>
                  <span v-else>{{"禁用"}}</span>
              </template>
          </el-table-column>
          <el-table-column
            align="right">
            <template slot="header">
                <el-tag type="success">机房信息管理功能模块选择区域</el-tag>
            </template>
            <template slot-scope="scope">
              <el-button size="mini" @click="dialogFormVisible = true" type="primary" plain>增加</el-button>
              <el-button size="mini" @click="dialogFormVisible2 = true" type="info" plain>编辑</el-button>
              <el-button size="mini" type="danger" plain @click="handleDelete(scope.$index, scope.row)">移除</el-button>
              <el-dialog title="课程增加" :visible.sync="dialogFormVisible">
                <el-form :model="addform">
                  <el-form-item label="机房名称" :label-width="formLabelWidth">
                    <el-input v-model="addform.name" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房类型" :label-width="formLabelWidth">
                    <el-input v-model="addform.type" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房大小" :label-width="formLabelWidth">
                    <el-input v-model="addform.size" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房位置" :label-width="formLabelWidth">
                    <el-input v-model="addform.location" autocomplete="off"></el-input>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible = false">取 消</el-button>
                  <el-button type="primary" @click="addSubmitForm">确 定</el-button>
                </div>
              </el-dialog>
              <el-dialog title="机房状态编辑" :visible.sync="dialogFormVisible2">
                <el-form :model="changform">
                  <el-form-item label="机房ID" :label-width="formLabelWidth">
                    <el-input v-model="changform.id" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房名字" :label-width="formLabelWidth">
                    <el-input v-model="changform.name" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房类型" :label-width="formLabelWidth">
                    <el-input v-model="changform.type" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房位置" :label-width="formLabelWidth">
                    <el-input v-model="changform.location" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="机房状态" :label-width="formLabelWidth">
                    <el-radio v-model="changform.status" label="1">可用</el-radio>
                    <el-radio v-model="changform.status" label="0">禁用</el-radio>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                  <el-button type="primary" @click="changSubmitForm">确 定</el-button>
                </div>
              </el-dialog>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      addform: {
        name: '',
        type: '',
        size: '',
        location: ''
      },
      changform: {
        id: '',
        type: '',
        size: '',
        location: '',
        name: '',
        status: ''
      },
      search: '',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      formLabelWidth: '120px'
    }
  },
  async created () {
    await this.$http.get(
      '/laboratory/findAll',
      { 'Content-Type': 'application/json' }
    ).then(res => {
      if (res.data.code === 200) {
        this.tableData = res.data.datas.labs
      }
    })
  },
  methods: {
    use (index, row) {
      this.tableData[index].isshow = '使用'
      console.log(index, row)
    },
    use1 (index, row) {
      this.tableData[index].isshow = '禁用'
      console.log(index, row)
    },
    async handleDelete (index, row) {
      console.log(this.tableData[index])
      await this.$http.get(
        '/laboratory/deleteById',
        this.tableData[index].id,
        { 'Content-Type': 'application/json' }
      ).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除机房成功')
        }
      })
      this.tableData.splice(index, 1)
    },
    async addSubmitForm () {
      await this.$http.get(
        '/laboratory/addLab',
        this.addform,
        { 'Content-Type': 'application/json' }
      ).then(res => {
        if (res.data.code === 200) {
          this.$message.success('增加机房成功')
          this.tableData.push(this.addform)
        }
      })
      this.dialogFormVisible = false
    },
    async changSubmitForm () {
      await this.$http.get(
        '/laboratory/updateLab',
        this.changform,
        { 'Content-Type': 'application/json' }
      ).then(res => {
        if (res.data.code === 200) {
          this.$message.success('更改机房成功')
        }
      })
      this.dialogFormVisible2 = false
    }
  }
}
</script>
<style>
.main{
  height: 70%;
  overflow: auto;
}
.table{
  background-color: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.chhead{
    height: 60px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  }
.el-table{
    position: relative;
    top: 30px;
    border: 1px solid rgba(220, 220, 220, 0.85);
}
.el-dialog__title{
  float: left;
  position: relative;
  left: 5%;
}
.el-form{
  position: relative;
  width: 80%;
  left: 0;
}
.el-form-item__content{
  width: 80%;
}
</style>
<style lang="less" scoped>

</style>
